<template>
	 <view>
	    <view class="goods-list">
	      <view v-for="(item, i) in goodsList" :key="i" @click="gotoDetail(item)">
	        <good-item :goods="item"></good-item>
	      </view>
	    </view>
	  </view>
</template>

<script>
	export default {
		data() {
			return {
				defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
				  ,
				  // 请求参数对象
				    queryObj: {
				      // 查询关键词
				      query: '',
				      // 商品分类Id
				      cid: '',
				      // 页码值
				      pagenum: 1,
				      // 每页显示多少条数据
				      pagesize: 10
				    },
				  goodsList:[],
				// 总数量，用来实现分页
				 isloading: false,
				 kw:"我最牛逼",
				total: 0
			}
		},
		
		onLoad(options) {
			// 将页面参数转存到 this.queryObj 对象中
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			
			this.getGoodsList()
		},
		
		// 触底的事件
		onReachBottom() {
			// 是否加载下一页
			if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('数据加载完毕！')
			// 判断是否 正在加载 
			if(this.isloading){
				return
			}
			
		  // 让页码值自增 +1
		  this.queryObj.pagenum += 1
		  // 重新获取列表数据
		  this.getGoodsList()
		},
		onPullDownRefresh() {
		  // 1. 重置关键数据
		  this.queryObj.pagenum = 1
		  this.total = 0
		  this.isloading = false
		  this.goodsList = []
		
		  // 2. 重新发起请求
		  this.getGoodsList(() => {uni.stopPullDownRefresh()})
		},
		methods: {
			// 获取商品列表数据的方法
			  async getGoodsList(callback) {
				
				this.isloading = true
			    // 发起请求
			    const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
			    if (res.meta.status !== 200) return uni.$showMsg()
			    // 为数据赋值
			    this.goodsList =[...this.goodsList,...res.message.goods] 
			    this.total = res.message.total
				callback && callback()
				// 加载完成
				this.isloading = false
			  },
			  
			  gotoDetail(goods) {
				  console.log("777",goods)
			    uni.navigateTo({
			      url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
			    })
			  },
		}
	}
</script>

<style scoped>
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
}

.goods-item .goods-item-left {
	margin-right: 5px;
}
.goods-item .goods-item-left .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
}
  
.goods-item    .goods-item-right {
	display: flex;
    flex-direction: column;
    justify-content: space-between;
}

 .goods-item  .goods-item-right  .goods-name {
	 font-size: 13px;
 }
  
.goods-item  .goods-item-right .goods-price {
      font-size: 16px;
      color: #d03e3d;
	  font-weight: 600;
}

</style>
